package com.web.treatment;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.domain.Treatments;
import com.service.TreatmentsService;
import com.util.AuthenticateHttpServlet;

@WebServlet("/Treatment/TreatmentDetail")
public class TreatmentDetail extends AuthenticateHttpServlet {

	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		TreatmentsService service = new TreatmentsService();
		String treatmentCode = request.getParameter("code");
		Treatments treatment = service.getTreatmentByCode(treatmentCode);
		String output = treatmentDetail(treatment);
		PrintWriter w = response.getWriter();
		w.println(output);
		w.close();

	}
	private String treatmentDetail(Treatments treatment) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("Treatment code: " + treatment.getTreatmentCode() + "<br/>");
		buffer.append("Treatment name: " + treatment.getTreatmentName() + "<br/>");
		buffer.append("Current Sales Price: " + treatment.getSalesPrice() + "<br/>");
		buffer.append("Product Description: " + treatment.getDescription() + "<br/>");
		buffer.append("<input type='hidden' name='code' value='" + treatment.getTreatmentCode() + "'>");
		return buffer.toString();
	}
	private String xmlTreatmentList(Treatments treatment) {
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("treatment");
		Element code = root.addElement("treatmentCode");
		code.setText(treatment.getTreatmentCode());
		Element name = root.addElement("productName");
		name.setText(treatment.getTreatmentName());
		Element salesPrice = root.addElement("salesPrice");
		salesPrice.setText(treatment.getSalesPrice()+ "");
		Element description = root.addElement("description");
		description.setText(treatment.getDescription());
		Element modeDate = root.addElement("modeDate");
		modeDate.setText(treatment.getModDate().toString());
		Element version = root.addElement("version");
		version.setText(treatment.getVersion()+"");
		return root.asXML();
	}
}
